﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;


namespace CommonLibrary
{
    public static class ImageClass
    {
        public static void CutImage(this Graphics g, int left, int top, int right, int bottom, Image image)
        {
            g.DrawImage(image, new Rectangle(0, 0, right < image.Width ? right : image.Width, bottom < image.Height ? bottom : image.Height), new Rectangle(left, top, right < image.Width ? right : image.Width, bottom < image.Height ? bottom : image.Height), GraphicsUnit.Pixel);
        }

        /// <summary>
        /// 从大图片中截取int left, int top, int right, int bottom，另存到newImage
        /// </summary>
        /// <param name="oldImage"></param>
        /// <param name="left"></param>
        /// <param name="top"></param>
        /// <param name="right"></param>
        /// <param name="bottom"></param>
        /// <param name="newImage"></param>
        public static void CutImage(string oldImage, int left, int top, int right, int bottom, string newImage)
        {
            Image image = Image.FromFile(oldImage);

            Bitmap newBmp = new Bitmap((right < image.Width ? right : image.Width) - left, (bottom < image.Height ? bottom : image.Height) - top);
            Graphics g = Graphics.FromImage(newBmp);
            g.Clear(Color.Empty);

            g.CutImage(left, top, right, bottom, image);

            newBmp.Save(newImage);

            newBmp.Dispose();
            g.Dispose();
            image.Dispose();
        }


        public static void CopyToBigImage(this Graphics g, int x, int y, Image image)
        {
            g.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
        }


    }

}


